package test.com.ampaiva.pattern.fsm;

import java.security.InvalidParameterException;

import test.com.ampaiva.pattern.fsm.FinalStateMachineTest.OvenEvent;
import test.com.ampaiva.pattern.fsm.Oven.OvenState;

import com.ampaiva.pattern.fsm.FinalStateMachine;
import com.ampaiva.pattern.fsm.FinalStateMachine.Action;

public class OvenController {
	public FinalStateMachine<OvenController, Oven, OvenState, OvenEvent> fsm = new FinalStateMachine<OvenController, Oven, OvenState, OvenEvent>(this);

	@Action(state = "OS_INIT")
	public OvenState onInit() {
		return OvenState.OS_IDLE;
	}

	@Action(state = "OS_IDLE")
	public OvenState onAnyAtIdle(OvenController oven, OvenState currentState, OvenEvent event) {
		if (currentState == OvenState.OS_IDLE)
			return OvenState.OS_COOKING;
		return currentState;
	}

	@Action(state = "OS_COOKING")
	public OvenState onAnyAtCooking(OvenEvent event) {
		if (event == OvenEvent.OE_OPEN)
			return OvenState.OS_COOKING_INTERRUPTED;
		return OvenState.OS_COOKING;
	}

	@Action()
	public OvenState onAny(OvenController oven, OvenState currentState, OvenEvent event) {
		if (currentState == OvenState.OS_COOKING_INTERRUPTED && event == OvenEvent.OE_CLOSE)
			return OvenState.OS_COOKING;
		throw new InvalidParameterException("Current State: " + currentState + " Event: " + event);
	}

}
